<template>
  <div>
    <div class="item">
      <MyEditor
        id="editor"
        ref="editor"
        :wordNum="TiLength"
        v-model="form.content"
        @change="onEditorChange($event)"
      >
      </MyEditor>
      <p v-if="warnShow" class="warnText">
        编辑内容不能超过10个字!
      </p>
      <span class="wordNumber ">{{ TiLength }}/10</span>
    </div>
  </div>
</template>
<script>
import MyEditor from "@/components/MyEditor/editor.vue";
export default {
  components: {
    MyEditor
  },
  data() {
    return {
      form: {
        content: ""
      },
      TiLength: 0,
      warnShow: false,
      editorHtml: null
    };
  },
  methods: {

    onEditorChange(e) {
       this.form.content=e.replace(/<p>\s*<\/p>/g, '').replace(/^<p>(.*)<\/p>$/, '$1')      
      console.log( this.form.content, "aaa");
 

      var reg = /<[^<>]+>/g; //去标签
      // var reg = /<(?!img).*?>/g //去除img以外的所有标签
      var value = e.replace(reg, "");
      value = value.replace(/&nbsp;/gi, ""); //将空格全部替换
      this.TiLength = value.length; //文字长度显示
      console.log(this.TiLength, "  this.TiLength");
      if (this.TiLength <= 10) {
        this.editorHtml = e;
        console.log(this.editorHtml, "this.editorHtml");
      }
      if (this.TiLength > 10) {
        //当长度大于10时,只截取10之前的内容并赋值
        this.$nextTick(() => {
          this.form.content = this.editorHtml;
        });
        this.warnShow = true;
        // this.$message.error("文字最多输入10字!");
        return false;
      }
    }
  }
};
</script>
<style  scoped>
.item {
  margin-top: 30px;
  width: 1100px;
  position: relative;
}
.wordNumber {
  color: #909399;
  background: #fff;
  text-align: right;
  z-index: 100;
  right: 10px;
  bottom: 5px;
  font-size: 12px;
  position: absolute;
}
.warnText {
  font-size: 14px;
  color: red;
  text-align: left;
}
</style>